﻿using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace UIWindows.Tools.Converter
{
    public class ResultToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                if (value is null || string.IsNullOrEmpty(value.ToString()))
                {
                    return Brushes.Transparent;
                }

                if (value is Enum)
                {
                    var enumInt = (byte)value;
                    if (enumInt == 0)
                    {
                        return Brushes.Transparent;
                    }
                    else if (enumInt == 1)
                    {
                        return Brushes.Green;
                    }
                    else
                    {
                        return Brushes.Red;
                    }
                }
                var v = value.ToString();

                if (int.TryParse(v, out int val))
                {
                    if (val == 0)
                    {
                        return Brushes.Transparent;
                    }
                    else if (val == 1)
                    {
                        return Brushes.Green;
                    }
                    else
                    {
                        return Brushes.Red;
                    }
                }
                else
                {
                    if (value.ToString().ToUpper() == "Undefined")
                    {
                        return Brushes.Transparent;
                    }
                    else if (value.ToString().ToUpper() == "TRUE" || value.ToString().ToUpper() == "OK" || value.ToString().ToUpper() == "01" || value.ToString().ToUpper().Contains("PASS"))
                    {
                        return Brushes.Green;
                    }
                    else if (value.ToString().ToUpper() == "FALSE" || value.ToString().ToUpper() == "NG" || value.ToString().ToUpper() == "FF")
                    {
                        return Brushes.Red;
                    }
                    else
                    {
                        return Brushes.Red;
                    }
                }
            }
            catch
            {
                return Brushes.Transparent;
            } 
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
